package com.example.mobile2121606109ycg0330homework1;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText edtTxtMainNum;
    private Button btnMainDel;
    private Button btnMainX;
    private Button btnMainChu;
    private Button btnMainA;
    private Button btnMain7;
    private Button btnMain8;
    private Button btnMain9;
    private Button btnMainJH;
    private Button btnMain4;
    private Button btnMain5;
    private Button btnMain6;
    private Button btnMainQM;
    private Button btnMain1;
    private Button btnMain2;
    private Button btnMain3;
    private Button btnMainJF;
    private Button btnMain0;
    private Button btnMainD;
    private Button btnMainDY;
//    private int a;
//    private int b;
//    private int sum;
//    private String FH;
    private double a = 0, b = 0;
    private double sum = 0;
    private int op = 0;
    private String opd = " ";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        btnMain0.setOnClickListener(this);
        btnMain1.setOnClickListener(this);
        btnMain2.setOnClickListener(this);
        btnMain3.setOnClickListener(this);
        btnMain4.setOnClickListener(this);
        btnMain5.setOnClickListener(this);
        btnMain6.setOnClickListener(this);
        btnMain7.setOnClickListener(this);
        btnMain8.setOnClickListener(this);
        btnMain9.setOnClickListener(this);
        btnMainJF.setOnClickListener(this);
        btnMainJH.setOnClickListener(this);
        btnMainChu.setOnClickListener(this);
        btnMainX.setOnClickListener(this);
        btnMainQM.setOnClickListener(this);
        btnMainDel.setOnClickListener(this);
        btnMainDY.setOnClickListener(this);
        btnMainD.setOnClickListener(this);

    }


    private void initView() {
        edtTxtMainNum = (EditText) findViewById(R.id.edtTxtMainNum);
        btnMainDel = (Button) findViewById(R.id.btnMainDel);
        btnMainX = (Button) findViewById(R.id.btnMainX);
        btnMainChu = (Button) findViewById(R.id.btnMainChu);
        btnMainA = (Button) findViewById(R.id.btnMainA);
        btnMain7 = (Button) findViewById(R.id.btnMain7);
        btnMain8 = (Button) findViewById(R.id.btnMain8);
        btnMain9 = (Button) findViewById(R.id.btnMain9);
        btnMainJH = (Button) findViewById(R.id.btnMainJH);
        btnMain4 = (Button) findViewById(R.id.btnMain4);
        btnMain5 = (Button) findViewById(R.id.btnMain5);
        btnMain6 = (Button) findViewById(R.id.btnMain6);
        btnMainQM = (Button) findViewById(R.id.btnMainQM);
        btnMain1 = (Button) findViewById(R.id.btnMain1);
        btnMain2 = (Button) findViewById(R.id.btnMain2);
        btnMain3 = (Button) findViewById(R.id.btnMain3);
        btnMainJF = (Button) findViewById(R.id.btnMainJF);
        btnMain0 = (Button) findViewById(R.id.btnMain0);
        btnMainD = (Button) findViewById(R.id.btnMainD);
        btnMainDY = (Button) findViewById(R.id.btnMainDY);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnMain0:
                String myString = edtTxtMainNum.getText().toString();
                myString += "0";
                edtTxtMainNum.setText(myString);
                break;
            case R.id.btnMain1:
                String myString1 = edtTxtMainNum.getText().toString();
                myString1 += "1";
                edtTxtMainNum.setText(myString1);
                break;
            case R.id.btnMain2:
                String myString2 = edtTxtMainNum.getText().toString();
                myString2 += "2";
                edtTxtMainNum.setText(myString2);
                break;
            case R.id.btnMain3:
                String myString3 = edtTxtMainNum.getText().toString();
                myString3 += "3";
                edtTxtMainNum.setText(myString3);
                break;
            case R.id.btnMain4:
                String myString4 = edtTxtMainNum.getText().toString();
                myString4 += "4";
                edtTxtMainNum.setText(myString4);
                break;
            case R.id.btnMain5:
                String myString5 = edtTxtMainNum.getText().toString();
                myString5 += "5";
                edtTxtMainNum.setText(myString5);
                break;
            case R.id.btnMain6:
                String myString6 = edtTxtMainNum.getText().toString();
                myString6 += "6";
                edtTxtMainNum.setText(myString6);
                break;
            case R.id.btnMain7:
                String myString7 = edtTxtMainNum.getText().toString();
                myString7 += "7";
                edtTxtMainNum.setText(myString7);
                break;
            case R.id.btnMain8:
                String myString8 = edtTxtMainNum.getText().toString();
                myString8 += "8";
                edtTxtMainNum.setText(myString8);
                break;
            case R.id.btnMain9:
                String myString9 = edtTxtMainNum.getText().toString();
                myString9 += "9";
                edtTxtMainNum.setText(myString9);
                break;
            case R.id.btnMainD:
                String myStringDot = edtTxtMainNum.getText().toString();
                myStringDot += ".";
                edtTxtMainNum.setText(myStringDot);
                break;
            case R.id.btnMainDel:
                edtTxtMainNum.setText("");
                break;
            case R.id.btnMainJF:
                String myStringJF = edtTxtMainNum.getText().toString();
                a = Double.valueOf(myStringJF);
                edtTxtMainNum.setText("+");
                op = 1;
                opd = "+";
                break;
            case R.id.btnMainJH:
                String myStringJH = edtTxtMainNum.getText().toString();
                a = Double.valueOf(myStringJH);
                edtTxtMainNum.setText("-");
                op = 2;
                opd = "-";
                break;
            case R.id.btnMainX:
                String myStringX = edtTxtMainNum.getText().toString();
                a = Double.valueOf(myStringX);
                edtTxtMainNum.setText("*");
                op = 3;
                opd = "*";
                break;
            case R.id.btnMainChu:
                String myStringCHU = edtTxtMainNum.getText().toString();
                a = Double.valueOf(myStringCHU);
                edtTxtMainNum.setText("/");
                op = 4;
                opd = "/";
                break;
            case R.id.btnMainQM:
                String myStringQM = edtTxtMainNum.getText().toString();
                a = Double.valueOf(myStringQM);
                edtTxtMainNum.setText("%");
                op = 5;
                opd = "%";
                break;
            case R.id.btnMainDY:
                String myStringDY = edtTxtMainNum.getText().toString();
                b = Double.valueOf(myStringDY);
                edtTxtMainNum.setText("=");
                switch (op) {
                    case 0:
                        sum = a;
                        break;
                    case 1:
                        sum = a + b;
                        break;
                    case 2:
                        sum = a - b;
                        break;
                    case 3:
                        sum = a * b;
                        break;
                    case 4:
                        if (b == 0)
                            sum = 0;
                        else
                            sum = a / b;
                        break;
                    case 5:
                        sum = a % b;
                        break;
                    default:
                        sum = 0;
                        break;
                }
                edtTxtMainNum.setText(Double.toString(a) + opd + Double.toString(b) + "=" + Double.toString(sum));
                break;
            default:
                break;
        }
    }
}